﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;


namespace TOSCOMMCreate
{
    public static class ClsEnumHelper
    {
        public static Dictionary<int, string> GetEnumDictionary<T>() where T : Enum
        {
            var dictionary = new Dictionary<int, string>();
            var type = typeof(T);

            foreach (var value in Enum.GetValues(type))
            {
                int intValue = (int)value;
                string description = GetEnumDescription(value as Enum);
                dictionary.Add(intValue, description);
            }

            return dictionary;
        }

        private static string GetEnumDescription(Enum value)
        {
            if (value == null)
                return string.Empty;

            var fieldInfo = value.GetType().GetField(value.ToString());
            if (fieldInfo == null)
                return value.ToString();

            var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }
    }
}
